package com.alibaba.cirtus.ots.application.adapter.ability.facade.service.sap;

import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.citrus.ots.common.exception.ProcessException;
import com.alibaba.citrus.ots.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnSapIf019Request;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnSapIf019SDO;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommSDO;
import com.epoch.app.otsapplicationadaptersdk.sap.mnsapif019.service.MnSapIf019Service;
import com.epoch.app.otsb2btradecenter.api.orderwrite.dto.SapDeleteSaleOrderRequest;
import com.epoch.app.otsb2btradecenter.api.orderwrite.service.OrderWriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MnSapIf019ServiceImpl implements MnSapIf019Service {

    private static Log log = Log.getLogger(MnSapIf019ServiceImpl.class);

    @Autowired
    private OrderWriteService orderWriteService;

    @ProcessInvoker(remark = "MnSapIf019Service", errorCode = "", printLog = true)
    @Override
    public MnSapCommResponse externalSyncSaleOrderDelete(MnSapIf019Request mnSapIf019Request) {
        log.info("MnSapIf019Request mnSapIf019Request:" + JSONObject.toJSONString(mnSapIf019Request));
        MnSapCommResponse response = new MnSapCommResponse();
        response.setInterfacePath(mnSapIf019Request.getMessageHeader().getInterfacePath());
        response.setMessageId(mnSapIf019Request.getMessageHeader().getMessageId());
        List<MnSapCommSDO> mnSapCommSDOS = new ArrayList<>();
        try {
            List<MnSapIf019SDO> mnSapIf019SDOS = mnSapIf019Request.getData();
            for (MnSapIf019SDO data : mnSapIf019SDOS) {
                try {
                    SapDeleteSaleOrderRequest request = new SapDeleteSaleOrderRequest();
                    request.setOutOrderId(data.getOutOrderId());
                    //request.setOrderType(data.getOrderType());
                    //request.setSaleOrganizationCode(data.getSaleOrganizationCode());
                    orderWriteService.sapDeleteSaleOrder(request);

                    MnSapCommSDO mnSapCommSDO = new MnSapCommSDO();
                    mnSapCommSDO.setStatus("S");
                    mnSapCommSDO.setMessage("订单：" + data.getOutOrderId() + "删除成功");
                    mnSapCommSDOS.add(mnSapCommSDO);
                } catch (FacadeException e) {
                    log.error("MnSapIf019ServiceImpl.externalSyncSaleOrderDelete business exception, error code:{}, error message:{}", e.getErrorCode(), e.getErrorMessage());
                    MnSapCommSDO mnSapCommSDO = new MnSapCommSDO();
                    mnSapCommSDO.setStatus("E");
                    mnSapCommSDO.setMessage(e.getErrorMessage());
                    mnSapCommSDOS.add(mnSapCommSDO);
                }
            }
        } catch (Exception e) {
            log.error("MnSapIf019ServiceImpl.externalSyncSaleOrderDelete unknow exception, error:{}", e);
            throw new ProcessException("OTS-05-001-10-16-047");
        }
        response.setData(mnSapCommSDOS);
        return response;
    }
}
